<?php
abstract class TabControl extends CmsBaseControl
{
    protected $parentId;
    protected $activeCssClass = 'active';

    public function TabControl($parent_id)
    {
        parent::__construct();
        $this->parentId = $parent_id;
    }

    public function Render()
    {
        $tabs = CmsMenuModel::GetInstance()->GetChilds($this->parentId);
        $this->Repeat('tab', $tabs, array($this,'FormatTab'));
        return parent::Render();
    }

    public function FormatTab($item)
    {
        if ($item['match'])
        {
            $match = String::RegExEscape($item['match']);
            preg_match("/$match/", RequestHandler::GetInstance()->url, $matches);
            $item['css_class'] = count($matches) > 0 ? $this->activeCssClass : null;
        }
        else
        {
            $url = Template::ReplaceArray(AppConfig::GetInstance()->GetReplaceValues(), $item['url']);
            $item['css_class'] = String::BeginWith($url, $_SERVER['REQUEST_URI']) ? $this->activeCssClass : null;
        }
        return $item;
    }
}
?>
